using HKB.Common;
using HKB.Data.Dal;
using HKB.Data.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace HKB.Data.Bll
{
	public class UserVipCard
	{
		private readonly HKB.Data.Dal.UserVipCard dal = new HKB.Data.Dal.UserVipCard();
		private static UserVipCard _instance = null;
		public static UserVipCard instance
		{
			get
			{
				if (UserVipCard._instance == null)
				{
					UserVipCard._instance = new UserVipCard();
				}
				return UserVipCard._instance;
			}
		}
		public bool Exists(long id)
		{
			return this.dal.Exists(id);
		}
		public long Add(HKB.Data.Model.UserVipCard model)
		{
			return this.dal.Add(model);
		}
		public bool Update(HKB.Data.Model.UserVipCard model)
		{
			return this.dal.Update(model);
		}
		public bool Delete(long id)
		{
			return this.dal.Delete(id);
		}
		public bool DeleteList(string idlist)
		{
			return this.dal.DeleteList(idlist);
		}
		public HKB.Data.Model.UserVipCard GetModel(long id)
		{
			return this.dal.GetModel(id);
		}
		public HKB.Data.Model.UserVipCard GetModelByCache(long id)
		{
			string cacheKey = "UserVipCardModel-" + id;
			object obj = DataCache.GetCache(cacheKey);
			if (obj == null)
			{
				try
				{
					obj = this.dal.GetModel(id);
					if (obj != null)
					{
						int configInt = ConfigHelper.GetConfigInt("ModelCache");
						DataCache.SetCache(cacheKey, obj, System.DateTime.Now.AddMinutes((double)configInt), System.TimeSpan.Zero);
					}
				}
				catch
				{
				}
			}
			return (HKB.Data.Model.UserVipCard)obj;
		}
		public DataSet GetList(string strWhere)
		{
			return this.dal.GetList(strWhere);
		}
		public DataSet GetList(int Top, string strWhere, string filedOrder)
		{
			return this.dal.GetList(Top, strWhere, filedOrder);
		}
		public System.Collections.Generic.List<HKB.Data.Model.UserVipCard> GetModelList(string strWhere)
		{
			DataSet list = this.dal.GetList(strWhere);
			return this.DataTableToList(list.Tables[0]);
		}
		public System.Collections.Generic.List<HKB.Data.Model.UserVipCard> DataTableToList(DataTable dt)
		{
			System.Collections.Generic.List<HKB.Data.Model.UserVipCard> list = new System.Collections.Generic.List<HKB.Data.Model.UserVipCard>();
			int count = dt.Rows.Count;
			if (count > 0)
			{
				for (int i = 0; i < count; i++)
				{
					HKB.Data.Model.UserVipCard userVipCard = this.dal.DataRowToModel(dt.Rows[i]);
					if (userVipCard != null)
					{
						list.Add(userVipCard);
					}
				}
			}
			return list;
		}
		public DataSet GetAllList()
		{
			return this.GetList("");
		}
		public int GetRecordCount(string strWhere)
		{
			return this.dal.GetRecordCount(strWhere);
		}
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			return this.dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
		}
		public DataSet GetListRowid(int Top, string strWhere, string filedOrder)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select ");
			if (Top > 0)
			{
				stringBuilder.Append(" top " + Top.ToString());
			}
			stringBuilder.Append(" ROW_NUMBER() OVER (ORDER BY id ASC) rowid,id,CardNumber,CardCategory,FaceValue,PurchaseUserID,PurchaseDate,Balance,CouponAmount ");
			stringBuilder.Append(" FROM MBF_UserVipCard ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			stringBuilder.Append(" order by " + filedOrder);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public DataSet PageOrderList(int PageSize, int PageIndex, string Orderfld, int OrderType, string strWhere)
		{
			return Conn.ExecProSpPageOrder("MBF_UserVipCard", "*", PageSize, PageIndex, Orderfld, OrderType, strWhere);
		}
		public long GetIDByCardNum(string CardNumber)
		{
			long result;
			if (CardNumber.Trim() == "")
			{
				result = 0L;
			}
			else
			{
				System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
				stringBuilder.AppendFormat("select Top 1 id FROM MBF_UserVipCard where CardNumber='{0}'", CardNumber);
				object single = DbHelperSQL.GetSingle(stringBuilder.ToString());
				if (single == null)
				{
					result = 0L;
				}
				else
				{
					result = System.Convert.ToInt64(single);
				}
			}
			return result;
		}
		public bool DecreaseBalance(long id, decimal BalanceToDec)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("update MBF_UserVipCard set ");
			stringBuilder.Append("Balance=Balance-@Balance,");
			stringBuilder.Append(" where id=@id");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@Balance", SqlDbType.Decimal, 9),
				new SqlParameter("@id", SqlDbType.BigInt, 8)
			};
			array[0].Value = BalanceToDec;
			array[1].Value = id;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
	}
}
